package com.android.base.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import com.android.base.application.BaseApplication;
import com.android.base.common.IPanel;
import com.android.base.dialog.DialogFactory;
import com.android.base.dialog.fragment.ProgressDialogFragment;
import com.android.base.jump.PanelForm;
import com.android.base.jump.PanelManager;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.Utils;
import me.imid.swipebacklayout.lib.app.SwipeBackActivityBase;
import me.imid.swipebacklayout.lib.app.SwipeBackActivityHelper;

public abstract class BaseActivity extends AppCompatActivity implements SwipeBackActivityBase, IPanel {

    //右滑删除Activity帮助类
    private SwipeBackActivityHelper mHelper;
    private int panelstatus = IPanel.PanelStatus.OK;

    protected DialogFactory mDialogFactory;

    protected DialogFactory getDialogFactory(){
        if(mDialogFactory == null){
            mDialogFactory = new DialogFactory(getSupportFragmentManager()) ;
        }
        return mDialogFactory;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        initOnCreate();
        super.onCreate(savedInstanceState);
        initSwipeBack();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mHelper.onPostCreate();
    }

    @Override
    protected void onResume() {
        BaseApplication.mAppHandle.activeActivity = this;
        super.onResume();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        PanelManager.getInstance().movePanelToTair(this.getPanelID());
        overridePendingTransition(0, 0);
    }

    @Override
    protected void onDestroy() {
        BaseApplication.mAppHandle.onAllActivityDestroy(this);
        if (getRootView() != null) {
            try {
                unbindDrawables(getRootView());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PanelManager.getInstance().unbindPanel(getPanelID());
        super.onDestroy();
    }

    public void unbindDrawables(View view) {
        if (view != null) {
            view.setBackgroundDrawable(null);
        }
        if (view instanceof ViewGroup) {
            ((ViewGroup) view).removeAllViews();
        }
    }

    @Override
    public View findViewById(int id) {
        View v = super.findViewById(id);
        if (v == null && mHelper != null)
            return mHelper.findViewById(id);
        return v;
    }

    @Override
    public SwipeBackLayout getSwipeBackLayout() {
        return mHelper.getSwipeBackLayout();
    }

    @Override
    public void setSwipeBackEnable(boolean enable) {
        getSwipeBackLayout().setEnableGesture(enable);//启动手势
    }

    @Override
    public void scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this);//将当前类转换为半透明效果（精髓所在）
        getSwipeBackLayout().scrollToFinishActivity();//关闭当前activity
    }

    @Override
    public Class<? extends BaseActivity> getPanelClass() {
        return this.getClass();
    }

    @Override
    public View getRootView() {
        return this.findViewById(android.R.id.content);
    }

    @Override
    public void returnBack() {
        PanelManager.getInstance().back();
    }

    @Override
    public void returnBackAndFinishStar() {

    }

    @Override
    public int getPanelLevel() {
        return PanelForm.panelform.get(getPanelID()).panelLevel;
    }

    @Override
    public int getPanelStatus() {
        return panelstatus;
    }

    @Override
    public void setPanelStatus(int panelstatus) {
        this.panelstatus = panelstatus;
    }

    /**
     * activity创建前
     */
    private void initOnCreate() {
        BaseApplication.mAppHandle.onAllActivityCreate(this);
        PanelManager.getInstance().bindPanel(getPanelID(),this);
        BaseApplication.mAppHandle.activeActivity = this;
    }
    //加载框
    protected DialogFragment mProgressDialog;
    /**
     * 显示加载框
     * */
    public void showLoadingDialog() {
        mProgressDialog = ProgressDialogFragment.createBuilder(this,getSupportFragmentManager()).setMessage("正在加载>>>").show();
    }
    /**
     * 隐藏加载框
     * */
    public void hideLoadingDialog() {
        if(mProgressDialog != null && mProgressDialog.getShowsDialog()){
            mProgressDialog.dismissAllowingStateLoss();
        }
    }

    /**
     * 右滑删除实例化
     */
    private void initSwipeBack() {
        mHelper = new SwipeBackActivityHelper(this);
        mHelper.onActivityCreate();
    }

    @Override
    public void finish() {
        PanelManager.getInstance().unbindPanel(getPanelID());
        super.finish();
    }
}
